package top.cross.webapp.common.mutilweb;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import top.cross.webapp.common.R;

public class ExabpleBaseWebViewActivity extends AppCompatActivity {

    String miniId;
    String url;
    boolean newLoad;

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        miniId = getIntent().getStringExtra("id");
        setContentView(R.layout.activity_exabple_base_web_view);
        loadInfo();
        webView = new WebView(this);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.e("console","onPageFinish "+url);
                Log.e("console","webView.isShown "+webView.isShown());
                if ("about:blank".equals(url)) {
                    webView.clearHistory();
                    webView.loadUrl(ExabpleBaseWebViewActivity.this.url);
                    return;
                }
                if (!webView.isShown()) {
                    webView.clearHistory();
                    webView.setVisibility(View.VISIBLE);
                }
            }
        });
        setContentView(webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("console", "WebView 现在是 onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("console", "WebView 现在是 onPause");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("console", "WebView 现在是 " + miniId);
        String oldId = miniId;
        setIntent(intent);
        loadInfo();
        Log.e("console", "WebView 接收到 " + miniId);
        Log.e("console", "WebView 重新加载 " + newLoad);
        Log.e("console", "WebView 重新加载 " + miniId.equals(oldId));
        if (!miniId.equals(oldId)) {
            webView.setVisibility(View.INVISIBLE);
            webView.loadUrl("about:blank");
            Log.e("console", "WebView 重新加载1 " + newLoad);
        } else if (newLoad) {
            Log.e("console", "WebView 重新加载2 " + newLoad);
            webView.loadUrl(url);
        }
    }

    private void loadInfo() {
        Intent intent = getIntent();
        miniId = intent.getStringExtra("id");
        url = intent.getStringExtra("url");
        newLoad = intent.getBooleanExtra("newLoad", false);
    }

    public String getMiniAppPKG() {
        return miniId;
    }

    /**
     * 删除所有cookie
     */
    private void removeAllCookie() {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();
        cookieManager.removeAllCookie();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.flush();
        }
        cookieSyncManager.sync();
        WebStorage.getInstance().deleteAllData();
    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            moveTaskToBack(false);
        }
    }
}
